package com.helloscala.web

import scala.collection.mutable

import net.liftweb.http._
import net.liftweb.common.Loggable
import net.liftweb.util.Helpers
import net.liftweb.http.provider.{HTTPRequest, HTTPParam}

object HelloURI extends Loggable {
  val invalidSiteId =
    Set("api", "favicon", "ajax_request", "comet_request", "assets", "root", "admin", "user", "guest", "static",
      "document", "tag", "article", "www", "demo", "about", "help", "change_password", "manage", "panel", "index", "doc")

  val rewrite: LiftRules.RewritePF = {
    case ValidSiteId(MyRewriteRequest(siteId, subs, rq)) =>
      userRewrite(siteId, subs, rq)

    case RewriteRequest(ParsePath(index :: Nil, suffix, true, _), _, _) if index == "index" || index == "" =>
      RewriteResponse(List("p", "index"))

    case rq@RewriteRequest(ParsePath(ValidSiteId(siteId) :: subs, suffix, true, _), _, _) =>
      userRewrite(siteId, subs, rq)

    case RewriteRequest(ParsePath("document" :: "new" :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "document", "edit"), Map("action" -> "new"))

    case RewriteRequest(ParsePath("document" :: documentId :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "document", "show"), Map("document_id" -> documentId))

    case RewriteRequest(ParsePath("document" :: documentId :: "edit" :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "document", "edit"), Map("document_id" -> documentId))

    case RewriteRequest(ParsePath("document" :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "document", "index"))

    case RewriteRequest(ParsePath("tag" :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "tag", "index"))

    case RewriteRequest(ParsePath("tag" :: tagId :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "tag", "detail"), Map("tag_id" -> tagId))

    case RewriteRequest(ParsePath("about" :: Nil, suffix, true, _), _, _) =>
      RewriteResponse(List("p", "about", "index"))

    case rq@RewriteRequest(ParsePath("404" :: Nil, _, true, _), _, _) =>
      val params = mutable.Map[String, String]()
      for (HTTPParam(name, value :: _) <- rq.httpRequest.params if value != "") params += name -> value
      RewriteResponse(List("p", "404"), params.toMap)

    case rq@RewriteRequest(ParsePath("500" :: Nil, _, true, _), _, _) =>
      val params = mutable.Map[String, String]()
      for (HTTPParam(name, value :: _) <- rq.httpRequest.params if value != "") params += name -> value
      RewriteResponse(List("p", "500"), params.toMap)
  }

  private[this] def userRewrite(userId: String, subs: List[String], rq: RewriteRequest) = {
    logger.debug(s"userRewrite: $userId subs: $subs")

    subs match {
      case Nil =>
        RewriteResponse(List("p", "user", "info"), Map("user_id" -> userId))

      case "index" :: Nil =>
        RewriteResponse(List("p", "user", "info"), Map("user_id" -> userId))

      case "detail" :: Nil =>
        RewriteResponse(List("p", "user", "detail"), Map("user_id" -> userId))

      case "change_password" :: Nil =>
        RewriteResponse(List("p", "user", "change_password"), Map("user_id" -> userId))

      case "document" :: "new" :: Nil =>
        RewriteResponse(List("p", "document", "edit"), Map("user_id" -> userId, "action" -> "new"))

      case "document" :: documentId :: Nil =>
        RewriteResponse(List("p", "document", "show"), Map("user_id" -> userId, "document_id" -> documentId))

      case "document" :: documentId :: "edit" :: Nil =>
        RewriteResponse(List("p", "document", "edit"), Map("user_id" -> userId, "document_id" -> documentId))

      case "tag" :: tagId :: Nil =>
        RewriteResponse(List("p", "user", "info"), Map("user_id" -> userId, "tag_id" -> tagId))

      case "tag" :: Nil =>
        RewriteResponse(List("p", "user", "tag"), Map("user_id" -> userId))

      case "micro_chat" :: id :: Nil =>
        RewriteResponse(List("p", "micro_chat", "detail"), Map("user_id" -> userId, "micro_chat_id" -> id))

      case _ =>
        //        throw new RuntimeException(s"userId: ${userId}; ${rq}")
        RewriteResponse(List("p", "404"), Map("user_id" -> userId, "content" -> subs.mkString(",")))
    }

  }

  // 绑定访问域名
  def validHostDomain(request: HTTPRequest): Boolean =
    W.siteLevel2(request).exists(hn => hn.name == "helloscala" && hn.domain == "com")

}
